package edu.hunau.cxb19.oop.class13.jdk7;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import org.junit.Test;

/**
 *
 * @author hb
 *
 *         2020-03-12 14:54:26
 */
public class DateTest {

	@Test
	public void now() {
		long now = System.currentTimeMillis();
		System.out.println(now);

		Date rightnow = new Date();
		System.out.println(rightnow);
		System.out.println(rightnow.getYear() + 1900);
		long time = rightnow.getTime();// 获得底层的毫秒数

		Calendar right = Calendar.getInstance();
		System.out.println(right);
		System.out.println(right.get(Calendar.YEAR));// 2020
		// 默认0表示1月，2表示3月
		System.out.println(right.get(Calendar.MONTH));// 2
		System.out.println(right.get(Calendar.DAY_OF_MONTH));// 12
		System.out.println(right.get(Calendar.DAY_OF_YEAR));// 72
	}

	@Test
	public void test02() {
		Calendar rightnow = Calendar.getInstance();// 2020-03-12
		rightnow.clear();// 将所有的属性的数据重置(1970-01-01 00:00:00)
		System.out.println(rightnow.get(Calendar.YEAR));
		// 设置为2019年.没有被设置的属性保存不变
		rightnow.set(Calendar.YEAR, 2019);// 2019-01-01
		System.out.println(rightnow);

	}

	@Test
	public void test03() {
		Calendar now = Calendar.getInstance();
		now.setLenient(false);// 关闭容错性
		now.set(2016, 7, 31);// 2016-8-31
		now.set(Calendar.MONTH, 8);
		// System.out.println(now.getTime());
		now.set(Calendar.DATE, 5);
		System.out.println(now.getTime());

	}

	@Test
	public void test04() {
		Calendar now = Calendar.getInstance();// 2020-3-12
		// 获得一个月之前
		now.add(Calendar.MONTH, -1);
		System.out.println(now.getTime());// 2020-2-12
		// 加上20天:如果当前属性的数据溢出，会自动向上一个属性进位
		now.roll(Calendar.DAY_OF_MONTH, 20);
		System.out.println(now.getTime());// 2020-3-3
	}

	@Test
	public void test05() {
		Date now = new Date();
		//实例化一个SimpleDateFormat
		SimpleDateFormat dateFormat = new SimpleDateFormat();
		System.out.println("默认的日期格式:"+dateFormat.format(now));//2020/3/12 下午4:24
		//指定预定义的格式
		dateFormat.applyPattern("yyyy-MM-dd HH:mm:ss");
		System.out.println(dateFormat.format(now));//2020-03-12 16:25:08
		dateFormat.applyPattern("yyyy'年'MM'月'dd'日' HH:mm:ss");
		System.out.println(dateFormat.format(now));//2020年03月12日 16:25:45
	}
	
	@Test
	public void test06() throws Exception {
		//实例化同时定义预定义格式
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd");
		//提供一个字符串
		String datas="2020/03/15";
		//转成Date类型
		Date d=dateFormat.parse(datas);
		System.out.println(d);
		
	}
	
	
	

}
